﻿using System;
using System.Xml;

namespace LdtJudge.DataEngine.Common.CommHelp
{
    public static class XpathConvertXmlDocument
    {

        /// <summary>
        /// 判断并添加节点
        /// </summary>
        /// <param name="xMLBase">xmldoc</param>
        /// <param name="objctpath">xpath将按照xpath循环添加</param>
        /// <returns>返回xdocument</returns>
        public static string ExtXMLnodeList(XmlDocument xMLBase, string objctpath)
        {
            /*将节点路径切片存放到数组中，之后循环该数组(倒序循环)，判断节点是否存在，如果存在则保存当前节点，之后添加当前节点子节点*/

            //节点路径切片
            objctpath = objctpath.Replace("/", "-");
            objctpath = objctpath.Replace(@"\", "-");
            string[] nodelist = objctpath.Split('-');
            string[] nodepath = new string[nodelist.Length];
            for (int loops = 1; loops <= nodelist.Length; loops++)
            {
                nodepath[loops - 1] = Arra_cut(nodelist, 0, loops, "/");
            }
            //遍历
            int addGoon = 0;//是否需要递归调用
            XmlNodeList parentNodelist = null;
            for (int loops = 0; loops < nodepath.Length; loops++)
            {
                if (nodepath[loops].Length < 1)
                {
                    continue;
                }
                XmlNodeList xmlNodelist = xMLBase.SelectNodes(nodepath[loops]);
                if (xmlNodelist.Count == 0)
                {
                    if (parentNodelist == null)
                    {
                        XmlElement rootNode = xMLBase.CreateElement(nodepath[loops].Replace("/", ""));
                        xMLBase.AppendChild(rootNode);
                        addGoon = 1;
                    }
                    else
                    {
                        //如果不存在则添加当前节点到父节点上
                        for (int inLoops = 0; inLoops < parentNodelist.Count; inLoops++)
                        {
                            string[] paths = nodepath[loops].Replace("/", "-").Split('-');
                            XmlElement elem = xMLBase.CreateElement(paths[paths.Length - 1]);
                            XmlNode pNode = parentNodelist[inLoops];
                            pNode.AppendChild(elem);
                        }
                        if (loops == nodepath.Length - 1)
                        {
                            addGoon = 0;
                            break;
                        }
                        else
                        {
                            addGoon = 1;
                        }
                    }
                }
                else
                {
                    parentNodelist = xmlNodelist;
                }
                if (addGoon == 1)
                {
                    ExtXMLnodeList(xMLBase, objctpath);
                }
            }

            return xMLBase.OuterXml;
        }

        /// <summary>
        /// 数组切片操作
        /// </summary>
        /// <param name="srcArr">源数组</param>
        /// <param name="startIndex">从源数组的哪个位置开始切</param>
        /// <param name="len">切片长度</param>
        /// <param name="connectChar">数组元素之间通过哪种字符拼接成字符串</param>
        /// <returns></returns>
        public static string Arra_cut(string[] srcArr, int startIndex, int len, string connectChar)
        {
            string[] temparr = new string[len];
            Array.Copy(srcArr, startIndex, temparr, 0, len);
            string tempstr = string.Join(connectChar, temparr);
            return tempstr;
        }

    }
}
